import os
from util.requestOwn import my_request
from celery.task import task

from apps.delay_missions import *
from django.conf import settings
from apps.missions.models import Mission
from apps.gateway.models import Sensor, Gateway
from util.simTool import SIMTool

UPDATE_GATEWAY = 1
UPDATE_SENSOR = 2
DOWN_SCRIPT = 3
DOWN_DELPLOY = 4
AGREEMENT = 5
MAIN = 6
CONFIG = 7


def cacultor_md5(file_type, path):
    # if file_type == UPDATE_GATEWAY:
    #     path = "".join([settings.UPDATE_GATEWAY_FILE_PATH, "/", path])
    # elif file_type == UPDATE_SENSOR:
    #     path = "".join([settings.UPDATE_SENSOR_FILE_PATH, "/", path])
    if file_type == DOWN_DELPLOY:
        path = "".join([settings.DEPLOY_JSON_FILE_DIR, "/", path])
    # elif file_type == AGREEMENT:
    #     path = "".join([settings.AGREEMENT_FILE_PATH, "/", path])
    # elif file_type == CONFIG:
    #     path = "".join([settings.CONFIG_FILE_PATH, "/", path])
    if file_type == AGREEMENT or file_type == CONFIG or file_type == MAIN:
        is_new = "1"
    else:
        is_new = "0"
    result = my_request.get_md5(path, is_new)
    md5 = result.get("md5")
    if md5:
        return path, md5
    else:
        return False, False


@task(name="reboot_gateway_task")
def reboot_gateway_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    data["mission"] = mission
    data["gateway_id"] = gateway.gateway_id
    data["gateway_client_id"] = gateway.client_id
    data["is_tac31"] = gateway.is_tac31
    reboot_gateway = RebootGateway(data)
    reboot_gateway.run()


@task(name="reboot_sensor_task")
def reboot_sensor_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    sensor = Sensor.objects.filter(id=mission.sensor_id).first()
    data["mission"] = mission
    data["gateway_id"] = gateway.gateway_id
    data["gateway_client_id"] = gateway.client_id
    data["is_tac31"] = gateway.is_tac31
    data["sid"] = sensor.number
    reboot_sensor = RebootSensor(data)
    reboot_sensor.run()


@task(name="read_gateway_task")
def read_gateway_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    data["mission"] = mission
    data["gateway_id"] = gateway.gateway_id
    data["gateway_client_id"] = gateway.client_id
    data["is_tac31"] = gateway.is_tac31
    read_gateway = ReadGateway(data)
    read_gateway.run()
    if gateway.is_tac31:
        if mission.is_again == 0 and mission.state == -1:
            mission.state = 0
            mission.save()
        else:
            read_string = mission.mission_result
            read_strings = read_string.split("+")
            singal = read_strings[4]
            gateway.singal = int(singal)
            gateway.save()


@task(name="read_sensor_task")
def read_sensor_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    sensor = Sensor.objects.filter(id=mission.sensor_id).first()
    data["mission"] = mission
    data["gateway_id"] = gateway.gateway_id
    data["gateway_client_id"] = gateway.client_id
    data["is_tac31"] = gateway.is_tac31
    data["sid"] = sensor.number
    read_sensor = ReadSensor(data)
    read_sensor.run()


@task(name="upload_gateway_task")
def upload_gateway_task(data):
    mission_id = data.get("mission_id")
    file_name = data.get("file_name")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    file_path, md5 = cacultor_md5(UPDATE_GATEWAY, file_name)
    if file_path:
        data["mission"] = mission
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["md5"] = md5
        data["file_path"] = file_path
        upload_gateway = UploadGateway(data)
        upload_gateway.run()
    else:
        mission.state = -1
        mission.failed_reason = "文件不存在"
        mission.save()


@task(name="upload_sensor_task")
def upload_sensor_task(data):
    mission_id = data.get("mission_id")
    file_name = data.get("file_name")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    sensor = Sensor.objects.filter(id=mission.sensor_id).first()
    file_path, md5 = cacultor_md5(UPDATE_SENSOR, file_name)
    if file_path:
        data["mission"] = mission
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["sid"] = sensor.number
        data["md5"] = md5
        data["file_path"] = file_path
        upload_sensor = UploadSensor(data)
        upload_sensor.run()
    else:
        mission.state = -1
        mission.failed_reason = "文件不存在"
        mission.save()


@task(name="down_script_task")
def down_script_task(data):
    mission_id = data.get("mission_id")
    file_name = data.get("file_name")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    path = "".join([gateway.script_name, "/", file_name])
    file_path, md5 = cacultor_md5(DOWN_SCRIPT, path)
    if file_path:
        data["mission"] = mission
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["md5"] = md5
        data["mission"] = mission
        data["file_path"] = file_path
        down_script = DownScript(data)
        down_script.run()
    else:
        mission.state = -1
        mission.failed_reason = "文件不存在"
        mission.save()


@task(name="log_task")
def log_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    sensor = Sensor.objects.filter(id=mission.sensor_id).first()
    if sensor:
        data["sid"] = sensor.number
        data["mission"] = mission
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["file_path"] = mission.file_path
        result = my_request.is_file_exit(mission.file_path)
        if not result:
            up_log = UploadLog(data)
            msg = up_log.run()
            if msg:
                result = my_request.is_file_exit(mission.file_path)
                if not result:
                    mission.state = -1
                    mission.failed_reason = "文件未上传成功"
                    mission.save()
        else:
            mission.state = 1
            mission.pct = 100
            mission.mission_result = "已存在日志"
            mission.save()
    else:
        mission.state = -1
        mission.failed_reason = "无传感器"
        mission.save()


@task(name="down_deploy_task")
def down_deploy_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    file_name = "".join([str(gateway.gateway_id), ".json"])
    file_path, md5 = cacultor_md5(DOWN_DELPLOY, file_name)
    if file_path:
        data["mission"] = mission
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["md5"] = md5
        data["mission"] = mission
        data["file_path"] = file_path
        down_deploy = DownDeploy(data)
        down_deploy.run()
    else:
        mission.state = -1
        mission.failed_reason = "文件不存在"
        mission.save()


@task(name="get_data_task")
def get_data_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    data["mission"] = mission
    data["gateway_id"] = gateway.gateway_id
    data["gateway_client_id"] = gateway.client_id
    data["is_tac31"] = gateway.is_tac31
    get_data = GetData(data)
    get_data.run()


@task(name="agreement_task")
def agreement_task(data):
    mission_id = data.get("mission_id")
    file_name = data.get("file_name")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    file_path, md5 = cacultor_md5(AGREEMENT, file_name)
    if file_path:
        data["mission"] = mission
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["md5"] = md5
        data["file_path"] = file_path
        agreementGateway = AgreementGateway(data)
        agreementGateway.run()
    else:
        mission.state = -1
        mission.failed_reason = "文件不存在"
        mission.save()


@task(name="main_task")
def main_task(data):
    mission_id = data.get("mission_id")
    file_name = data.get("file_name")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    file_path, md5 = cacultor_md5(MAIN, file_name)
    if file_path:
        data["mission"] = mission
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["md5"] = md5
        data["file_path"] = file_path
        main_gateway = MainGateway(data)
        main_gateway.run()
    else:
        mission.state = -1
        mission.failed_reason = "文件不存在"
        mission.save()


@task(name="config_task")
def config_task(data):
    mission_id = data.get("mission_id")
    file_name = data.get("file_name")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    file_path, md5 = cacultor_md5(CONFIG, file_name)
    if file_path:
        data["mission"] = mission
        data["gateway_id"] = gateway.gateway_id
        data["gateway_client_id"] = gateway.client_id
        data["is_tac31"] = gateway.is_tac31
        data["md5"] = md5
        data["mission"] = mission
        data["file_path"] = file_path
        down_deploy = DownDeploy(data)
        down_deploy.run()
    else:
        mission.state = -1
        mission.failed_reason = "文件不存在"
        mission.save()


@task(name="poweroff_task")
def poweroff_gateway_task(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    data["mission"] = mission
    data["gateway_id"] = gateway.gateway_id
    data["gateway_client_id"] = gateway.client_id
    data["is_tac31"] = gateway.is_tac31
    poweroff = PowerOff(data)
    poweroff.run()


@task(name="query_sim")
def query_sim(data):
    mission_id = data.get("mission_id")
    mission = Mission.objects.filter(id=mission_id).first()
    gateway = Gateway.objects.filter(key=mission.key).first()
    sim_number = gateway.sim_number
    sim_tool = SIMTool()
    result = sim_tool.get_sim(sim_number)
    if result:
        mission.state = 1
        mission.pct = 100
        gateway.sim_status = result
    else:
        message = sim_tool.get_message()
        mission.state = -1
        mission.failed_reason = message
    mission.save()
    gateway.save()


@task(name="check_sim_charge")
def check_sim_charge():
    gateways = Gateway.objects.all()
    for gateway in gateways:
        sim_number = gateway.sim_number
        sim_tool = SIMTool()
        result = sim_tool.get_sim(sim_number)
        if result:
            gateway.sim_status = result
            gateway.save()
